package com.imcrl.effect.help
{
	import com.imcrl.effect.GenieEffect;
	
	import starling.animation.Tween;
	import starling.core.Starling;
	
	public class GenieTween
	{
		private static const ICON_SIZE:int=30;
		private static const SCALE_TIME:Number=.3;
		private static const HIDE_TIME:Number=1;
		
		public var onUpdate:Function;
		
		public var cpTL : ControlePoint;
		public var cpTR : ControlePoint;
		public var cpBL : ControlePoint;
		public var cpBR : ControlePoint;
		public var dh:int;
		public var dw:int;
		public var hdh:int;
		public var hdw:int;
		
		private var x:int;
		private var y:int;
		private var _secondVar:Number;
		private var tween:Tween;
		public function GenieTween(target:GenieEffect)
		{
		
		}
		
		public function moveTo(x:int,y:int):void{
			this.x=x;
			this.y=y;
			
			var tween:Tween;
			tween=new Tween(cpTL,SCALE_TIME);
			tween.onUpdate=firstUpdate;
			tween.moveTo(x,y);
			Starling.current.juggler.add(tween);
	
			var by:int=y+ICON_SIZE;
			tween=new Tween(cpBL,SCALE_TIME);
			tween.onUpdate=onUpdate;
			tween.moveTo(x,by);
			
			tween.onComplete=secondAction;
			
			Starling.current.juggler.add(tween);
		}

		public function firstUpdate():void
		{
			var scaleLeftY:int=(cpTL.y+dh-cpBL.y)/dh*100;
			if(scaleLeftY<=0){
				trace("a");
				return;
			}
			var v:Number=1-scaleLeftY/100;
			cpTL.scaleY=v;
			cpBL.scaleY=v;
		}

		public function secondAction():void{
			
			//x+=300;
			
			var tween:Tween;
			var by:int;
			by=y+ICON_SIZE;
			
			tween=new Tween(cpTL,HIDE_TIME);
			//tween.onUpdate=firstUpdate;
			tween.moveTo(-dw,y);
			//Starling.current.juggler.add(tween);
			
			
			tween=new Tween(cpBL,HIDE_TIME);
			//tween.onUpdate=onUpdate;
			tween.moveTo(-dw,by);
			//Starling.current.juggler.add(tween);
			
			tween=new Tween(cpTR,HIDE_TIME);
			tween.onUpdate=secondUpdate;
			tween.moveTo(cpTR.x,y);
			Starling.current.juggler.add(tween);
		   

			tween=new Tween(cpBR,HIDE_TIME);
			tween.onUpdate=onUpdate;
			tween.moveTo(cpBR.x,by);
			Starling.current.juggler.add(tween);
		}
		private function secondUpdate():void{
			var scaleRightY:int=(cpTR.y+dh-cpBR.y)/dh*100;
			if(scaleRightY<=0){
				trace("b");
				return;
			}
			var v:Number=1-scaleRightY/100;
			cpTR.scaleY=v;
			cpBR.scaleY=v;
		}

		
	}
}